package com.tao.china.taobaoapi.store;

import java.util.concurrent.Callable;

import com.tao.china.biz.domain.Product;
import com.tao.china.biz.jdbc.dao.ProductDao;
import com.tao.china.biz.jdbc.dao.impl.ProductDaoImpl;
import com.tao.china.taobaoapi.request.ItemsSearch;
import com.tao.china.taobaoapi.result.ItemSearchResult;
import com.taobao.api.domain.Item;
import com.taobao.api.response.ItemsSearchResponse;

/**
 * @author liuchao
 * @version $Id: ItemsSearchCallable.java,v 0.1 2012-9-21 下午11:31:18 liuchao Exp $
 */
public class ItemsSearchCallable implements Callable<ItemSearchResult> {
    
    private     Long cid;
    private     Long pageNo;
    private     ProductDao productDAO ;
    public static final Long  PAGE_SIZE = 200L;
    
    public  ItemsSearchCallable(Long cid,Long pageNo){
        this.cid = cid;
        this.pageNo = pageNo;
        productDAO = new ProductDaoImpl();
    }
    

    @Override
    public ItemSearchResult call() throws Exception {
        ItemSearchResult result = new ItemSearchResult();
        result.setCid(cid);
        result.setPageNo(pageNo);
        
        try{
            ItemsSearchResponse searchRes = ItemsSearch.getSearchRes( cid, pageNo, PAGE_SIZE );
            if( null != searchRes.getErrorCode()){
                result.setErrorNOInfo(0001, "API调用错误");
            }
            if( searchRes.getItemSearch().getItems() != null && searchRes.getItemSearch().getItems().size() > 0){
                for(    Item item : searchRes.getItemSearch().getItems()  ){
                    Long iid = item.getNumIid();
                    System.out.println("---------iid---------"+iid);
                    if( null != iid){
                        Product product = new Product();
                        product.setProductId( item.getNumIid() );
                        product.setCatId( item.getCid().intValue() );
                        product.setPrice( Double.parseDouble( item.getPrice()));
                        product.setPicUrl( item.getPicUrl() );
                        product.setTitle( item.getTitle() );
                        product.setNickName( item.getNick() );
                        product.setVolume( item.getVolume().intValue() );
                        product.setStates(0001);
                        //插入
                        productDAO.insertBySearchAPI(   item.getNumIid()%100, product   );
                    }
                }
            }
        }
            catch( Exception ex){
            result.setErrorNOInfo(0001, ex.getMessage());
        }
       
        return result;
    }

}
